package com.huak.zookeeper.connection;

import lombok.extern.slf4j.Slf4j;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

/**连接Zkserver
 * @author liuhuan
 * @date 2018/9/12 14:15
 * @description
 */
@Slf4j
public class ZkConnection implements Watcher{

    //public static final String ZK_SERVER_PATH="172.16.9.12:2181";

    //public static final String ZK_SERVER_PATH="172.16.9.12:2181,172.16.9.13:2181";
    public static final String ZK_SERVER_PATH="192.168.0.128:2181";


    public static final int TIMEOUT=5000;

    public static void main(String[] args) throws Exception{
        /**
         * 实例化zooKeeper对象
         */
        ZooKeeper zooKeeper=new ZooKeeper(ZK_SERVER_PATH,TIMEOUT,new ZkConnection());
        log.info("开始连接客户端");
        log.info("连接状态:{}",zooKeeper.getState());
        Thread.sleep(2000); //连接需要时间大约0.5s

        log.info("连接状态:{}",zooKeeper.getState());
    }

    @Override
    public void process(WatchedEvent watchedEvent) {
        log.info("接收到watch的通知:{} ",watchedEvent);
    }
}
